package com.example.xinlijiankangsys.controller;

import com.example.xinlijiankangsys.pojo.Comment;
import com.example.xinlijiankangsys.pojo.Result;
import com.example.xinlijiankangsys.service.CommentService;
import com.example.xinlijiankangsys.util.ReturnResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping
    public Result createComment(@RequestBody Comment comment) {
        try {
            Comment createdComment = commentService.create(comment);
            return ReturnResultUtil.success();
        } catch (Exception e) {
            return ReturnResultUtil.internalServerError(e.getMessage());
        }
    }

    @PostMapping("/{commentId}/like")
    public Result likeComment(@PathVariable Long commentId) {
        try {
            commentService.like(commentId);
            return ReturnResultUtil.success();
        } catch (Exception e) {
            return ReturnResultUtil.internalServerError(e.getMessage());
        }
    }

    @DeleteMapping("/{commentId}")
    public Result deleteComment(@PathVariable Long commentId) {
        try {
            commentService.delete(commentId);
            return ReturnResultUtil.success();
        } catch (Exception e) {
            return ReturnResultUtil.internalServerError(e.getMessage());
        }
    }
}